﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;


namespace AutofacDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //容器
            var builder = new ContainerBuilder();
            builder.RegisterType<DBManager>();
            //注册类型及其实例 001
            //builder.RegisterType<SqlDAL>().As<IDAL>();
             
            //多个类映射同一个接口的时候，命名 002
            builder.RegisterType<OracleDAL>().Named<IDAL>("oracle");
            builder.RegisterType<SqlDAL>().Named<IDAL>("sql");

            using(var container = builder.Build())
            {
                /* 001
                var manager = container.Resolve<DBManager>();

                //解析IDAL的实例SqlDAL
                //SqlDAL sqlDAL = (SqlDAL)container.Resolve<IDAL>();

                var result = manager.Add("insert into Persons values('zhang',19)");
                Console.WriteLine(result);
                */
                //002
                var sql = (SqlDAL)container.ResolveNamed<IDAL>("sql");
                var oracle = (OracleDAL)container.ResolveNamed<IDAL>("oracle");

                var sqlresult = sql.Insert("insert into Persons values('lisi',22)");
                var oracleresult = oracle.Insert("insert into Persons values('wangwu',24)");

                Console.WriteLine(sqlresult);
                Console.WriteLine(oracleresult);
            }

            

            Console.ReadLine();


        }
    }

    public class DBManager
    {
        IDAL _dal;
        public DBManager(IDAL dal)
        {
            _dal = dal;
        }

        public string Add(string commandText)
        {
            return _dal.Insert(commandText);
        }
    }
}
